import request from "@/utils/request";
import store from '@/store'
/**
 * API封装成方法
 * */

/**
 * 登录
 * */
export function userLogin(username,password,authCode){
    return request({
        url: 'api/login',
        method: 'post',
        params:{
            username: username,
            password: password,
            authCode: authCode
        }
    })
}

/**
 * 登录状态 token验证
 * */
export function verifyAccount(){
    return request({
        url: 'api/common/account/validate',
        method: 'get'
    })
}

/**
 * 用户注册
 * */
export function signIn(username,password,email,code){
    return request({
        url: 'api/register',
        method: 'post',
        params:{
            username:username,
            password:password,
            email:email,
            code:code
        }
    })
}

/*上传图片*/
export function uploadImage(formData) {
    return request({
        url: "/api/file/upload",
        method: 'post',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

/*获取type list*/
export function getTypes() {
    return request({
        url: "/api/type/list",
        method: 'get',
    })
}

//获取tag
export function getTags() {
    return request({
        url: "/api/tag/list",
        method: 'get',
        params: {
            ownerId: store.state.user.id,
        }
    })
}

export function addTag(tagName) {
    return request({
        url: "/api/tag/add",
        method: 'post',
        params: {
            name: tagName,
            owner: store.state.user.id,
        }
    })
}

/*保存博客*/
export function saveBlog(blogInfo) {
    return request({
        url: "/api/blog/save",
        method: 'post',
        dataType:'json',
        params: {
            id: blogInfo.id,
            content: blogInfo.content,
            title: blogInfo.title,
            owner: blogInfo.owner,
            publish: blogInfo.publish,
            typeId: blogInfo.typeId,
            tagId: blogInfo.tagId,
            banner: blogInfo.banner
        }
    })
}

export function getBlogList(current, size) {
    return request({
        url: '/api/blog/listvo',
        method: 'get',
        params: {
            current: current,
            size: size
        }
    })
}

export function getBlog(id) {
    return request({
        url: '/api/blog/get',
        method: 'get',
        params: {
            id: id
        }
    })
}

export function getBlogComment(blogId) {
    return request({
        url: '/api/comment/list',
        method: 'get',
        params: {
            blogId: blogId
        }
    })
}
export function getBlogCommentLazy(blogId,parentId) {
    return request({
        url: '/api/comment/lazylist',
        method: 'get',
        params: {
            blogId: blogId,
            parentId:parentId
        }
    })
}
export function saveComment(blogId,parentId,content) {
    return request({
        url: '/api/comment/save',
        method: 'post',
        params: {
            blogId: blogId,
            parentId:parentId,
            content:content,
            userId:store.state.user.id
        }
    })
}

export function saveFavour(blogId) {
    return request({
        url: '/api/favour/save',
        method: 'post',
        params: {
            blogId: blogId,
            userId:store.state.user.id
        }
    })
}